
package com.zcp.wizard;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * WidgetProvider
 */
public final class WidgetProvider extends AppWidgetProvider {

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {

        super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);

        WidgetUtils.updateSystemStats(context,false);
        //主动提醒更新，由于在加第二个widget的时候，由于不会在启动service，这时候要等电池事件来才会更新widget，所以主动提醒更新
        Intent intent=new Intent(Constants.UPDATE_WIDGET);
        context.sendBroadcast(intent);
        
    }
    @Override
    public void onEnabled(Context context) {
        //这个不管有多少个widget都只是被执行一次
        SharedPreferences share = context.getSharedPreferences(Constants.WIZARD_SETTINGS,
                Context.MODE_PRIVATE);
        Editor edit = share.edit();
        edit.putInt(Constants.HAVE_SET_WIDGET, 1);
        edit.commit();
//        Log.i("tag1", "HAVE_SET_WIDGET update"+share.getInt(Constants.HAVE_SET_WIDGET, 0));
        startService(context);
    }
    @Override
    public void onDisabled(Context context) {
        //这个不管有多少个widget都只是被执行一次
        SharedPreferences share = context.getSharedPreferences(Constants.WIZARD_SETTINGS,
                Context.MODE_PRIVATE);
        Editor edit = share.edit();
        edit.putInt(Constants.HAVE_SET_WIDGET, 0);
        edit.commit();
//        Log.i("tag1", "HAVE_SET_WIDGET onDeleted"+share.getInt(Constants.HAVE_SET_WIDGET, 0));
        stopService(context);
    }
    
    private void stopService(Context context) {
//        Log.i("tag", "stop service");
        Intent stopIntent = new Intent(context, WidgetStatusObserverService.class);
        context.stopService(stopIntent);
    }

    private void startService(Context context) {
//        Log.i("tag", "start service");
        Intent startIntent = new Intent(context, WidgetStatusObserverService.class);
        context.startService(startIntent);
    }

}
